import { MapData, type FeatureDataInterface, type FeatureInterface, type MapDataInterface } from "@/class/map/MapData";
import { convert_gcj02_To_Gps84, convert_gps84_To_Gcj02, MapCoordSys } from "@/class/map/myMapOpt";
import { reqDeleteMapData } from "@/api/mapdata";
import { reqPostMarker, reqPutMarker } from "@/api/marker"

import { requestGetData, requestIsSuccess } from "@/api/request";
import { getUUID, toJson } from "@/utils/utils";
import { isPosition } from "@/utils/map";

export class MarkerData implements FeatureDataInterface {
    featureData: MapDataInterface
    featureMap: Marker
    constructor({ featureData, featureMap }: {
        featureData: MapData
        featureMap: Marker
    }) {
        this.featureData = new MapData(featureData)
        this.featureMap = new Marker(featureMap)
    }

    /**
     * 移动到该要素
     * @param map
     */
    moveTo(map: AMap.Map) {
        /*const element = this.getDateInMap(map)
        var bounds = element.getBounds();
        map.setBounds(bounds)*/
        map.setCenter(this.featureMap.position as [number,number])
        this.blinkData(map)
        /*const position = this.marker.position
        map.setCenter(position)*/
    }

    /**
     * 添加要素，并返回地图要素
     * @param map
     * @returns {*}
     */
    addToMap(map: AMap.Map, mapCoordSys: MapCoordSys) {

        if (this.featureData.id === undefined) {
            this.featureData.id = getUUID();
        }
        if (this.featureMap.id === undefined) {
            this.featureMap.id = getUUID();
        } const result = this.getBaiduMapOpt(mapCoordSys)
        const feature = new AMap.Marker(result);
        feature.setExtData({
            id: this.featureData.id,
            name:this.featureData.name
        })
        map.add([feature])
        return feature
    }

    /**
     * 返回地图需要的数据，如果坐标不是CJ02，转换为CJ02
     * @returns {Promise<any>}
     */
    getBaiduMapOpt(mapCoordSys: MapCoordSys) {
        const opt = JSON.parse(JSON.stringify(this.featureMap))
        //坐标转换
        switch (mapCoordSys) {
            case MapCoordSys.CJ02:
                if (this.featureData.coordsys !== MapCoordSys.CJ02)
                    opt.position = convert_gps84_To_Gcj02(opt.position)
                break;
            case MapCoordSys.GPS:
                if (this.featureData.coordsys !== MapCoordSys.GPS)
                    opt.position = convert_gcj02_To_Gps84(opt.position)
                break;
        }
        return opt
    }

    /**
     * 更新点数据
     * @returns true;false
     */
    async update() {
        const result = await reqPutMarker(this)
        return true
    }
    /**
     * 添加点数据
     * @returns true;false
     */
    async upload() {
        const result = await reqPostMarker(this)
        return true
    }

    /**
     * 返回要素的原始设置
     * @returns {Marker}
     */
    getElement() {
        return this.featureMap
    }

    /**
     * 判断能否正确添加到该地图
     * @returns {boolean}
     */
    isElement() {
        return (this.featureMap.position as [number,number]).length > 0
    }

    /**
     * 返回复制对象
     * @returns {MarkerData}
     */
    getCopy() {
        return new MarkerData(JSON.parse(JSON.stringify(this)))
    }

    /**
     * 返回地图中的对象
     * @param map
     * @returns {*}
     */
    getDateInMap(map: any) {
        return map.getAllOverlays().find((item: any) => {
            return item.getOptions().extData.id === this.featureData.id
        })
    }

    /**
     * 删除map中的元素
     * @param map
     */
    deleteDataInMap(map: any) {
        map.remove(this.getDateInMap(map))
    }

    /**
     * 闪烁该要素
     * @param map
     */
    blinkData(map: any) {
        var mapData = this.getDateInMap(map);
        //闪烁次数
        let blinkTimes = 6
        let currentTimes = 0
        const interval = setInterval(() => {
            if (currentTimes < blinkTimes) {
                if (mapData.getVisible()) {
                    mapData.setOptions({
                        "visible": false
                    })
                } else {
                    mapData.setOptions({
                        "visible": true
                    })
                }
                currentTimes++
            } else {
                clearInterval(interval)
                mapData.setOptions({
                    "visible": true
                })
            }
        }, 100)
    }

    /**
     * 下载json数据
     */
    getJSON() {
        toJson(this.featureData.name as string, this)
    }
    /**
     * 删除要素
     */
    deleteElement() {
        return reqDeleteMapData(this.featureData.id as string)
    }
    /**
     * 设置坐标点
     * @param path 坐标点
     * @returns 
     */
    setPath(path: any[]) {
        if (isPosition(path)) {
            let position = path as [number, number]
            this.featureMap.position = position
        }
    }
}
export class Marker implements FeatureInterface {
    id?: string
    position?: [number, number]
    extData?: object
    constructor({ id, position, extData }: { id?: string, position?: [number, number], extData?: object }) {
        this.id = id
        this.position = position
        this.extData = extData
    }
}